<?php defined( '_JEXEC' ) or die( 'Restricted access' ); ?><?php defined( '_SEXEC' ) or die( 'Restricted access' ); ?>
<?php
	if(!isset($_SESSION['username']) && !isset($_SESSION['usertype']))
		header("location: index.php");
?>
<?php	

if(isset($submit))
{
	switch(trim($submit))
	{
		case "Save":
				b($booking_date,"Enter Booking Date");
				b($cn_no,"Enter Courier Number");
				$sql="select * from inventories where cn_no = '$cn_no'";
				$result = mysql_query($sql);
				if(mysql_num_rows($result) > 0){
					$arr[] = "Courier Number already exists in database";
				}
				b($weight,"Enter Courier Weight");
				i($weight,"Courier Weight must be in Number");
				i($invoice_value,"Invoice Value must be in Number");
				if(count($arr) == 0) {
					$booking_date = db_date_format($booking_date);
					$numerics = preg_split('/[a-zA-Z]/', $cn_no, -1, PREG_SPLIT_NO_EMPTY);
					$characters = preg_split('/[0-9]/', $cn_no, -1, PREG_SPLIT_NO_EMPTY);
					
					$characters = $characters[0];
					$numerics = $numerics[0];
									
					for($cn=0;$cn<$cn_no_to;$cn++){
						if(substr($numerics, 0, 1) == 0)
							$number = '0' . ($numerics + $cn);
						else
							$number = $numerics + $cn;
						$cn_no = $characters . $number;
						$sql = "insert into inventories(company_id,booking_date,cn_no,invoice_no,invoice_value,destination_id,document_id,weight,weight_unit,cour_charge,total_charge,status,datetime) values(\"$company_id\",\"$booking_date\",\"$cn_no\",\"$invoice_no\",\"$invoice_value\",\"$destination_id\",\"$document_id\",\"$weight\",\"$weight_unit\",\"$cour_charge\",\"$total_charge\",\"$status\",now())";
						mysql_query($sql);
					}

					$msg = "Inventory added succesful";
					$company_id=$booking_date=$cn_no=$invoice_no=$invoice_value=$destination_id=$document_id=$weight=$weight_unit=$cour_charge=$total_charge=$status="";
				}
				break;
		case "Update":
				if($step == 2) {
					b($booking_date,"Enter Booking Date");
					b($cn_no,"Enter Courier Number");
					$sql="select * from inventories where cn_no = '$cn_no' and  id != '$id'";
					$result = mysql_query($sql);
					if(mysql_num_rows($result) > 0){
						$arr[] = "Courier Number already exists in database";
					}
					b($weight,"Enter Courier Weight");
					i($weight,"Courier Weight must be in Number");
					i($invoice_value,"Invoice Value must be in Number");
					if(count($arr) == 0) {
						$booking_date = db_date_format($booking_date);
						$sql = "update inventories set company_id=\"$company_id\", booking_date=\"$booking_date\", cn_no=\"$cn_no\", invoice_no=\"$invoice_no\", invoice_value=\"$invoice_value\", destination_id=\"$destination_id\", document_id=\"$document_id\", weight=\"$weight\", weight_unit=\"$weight_unit\", cour_charge=\"$cour_charge\", total_charge=\"$total_charge\", status=\"$status\", datetime=now() where id = '$id'";
						mysql_query($sql);
						$msg = "Inventory updated succesful";
						$company_id=$booking_date=$cn_no=$invoice_no=$invoice_value=$destination_id=$document_id=$weight=$weight_unit=$cour_charge=$total_charge=$status="";
					} else {
				  		$btval="     Update     ";
				 	}
				} 
				else if(isset($eid)) {
					$sql="select * from inventories where id = '$eid'";
					$result = mysql_query($sql);
					$row = mysql_fetch_array($result);
					$id = $row['id'];
					$company_id = $row['company_id'];
					$booking_date = $row['booking_date'];
					$booking_date = db_date_format($booking_date);
					$cn_no = $row['cn_no'];
					$invoice_no = $row['invoice_no'];
					$invoice_value = $row['invoice_value'];
					$destination_id = $row['destination_id'];
					$document_id = $row['document_id'];
					$weight = $row['weight'];
					$weight_unit = $row['weight_unit'];
					$cour_charge = $row['cour_charge'];
					$total_charge = $row['total_charge'];
					$status = $row['status'];
					$step=2;
					$btval="     Update     ";
				}
				break;
		case "Delete":
				$sql = "delete from inventories where id = '$eid'";
				mysql_query($sql);
				$msg = "Inventory delete succesful";
				break;
	}
}

if(!isset($btval))
	$btval="     Save     ";
	

$sql = "select id, companyname from companies where status = 1 order by companyname";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
	$company_arr[$row['id']] = $row['companyname'];
}

$sql = "select * from destinations where status = 1 order by destination_type, destination";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
	$destination_arr[$destination_type_arr[$row['destination_type']]][$row['id']] = $row['destination'];
}

?>

<script>
function GetCharge(){
	var cid = $("#company_id").val();
	var desid = $("#destination_id").val();
	var docid = $("#document_id").val();
	var slabfor = $("#slab_for").val();
	var wght = $("#weight").val();
	var wghtun = $("#weight_unit").val();
	if((wght != '') && (!isNaN(wght))){
		$.ajax({
			url: "charge.php",
			data: "company_id=" + cid + "&destination_id=" + desid + "&weight=" + wght + "&weight_unit=" + wghtun + "&document_id=" + docid + "&slab_for=" + slabfor,
			dataType: "json",
			success: function(data1){
				$('#cour_charge').val(data1.fright);
				$('#total_charge').val(data1.total);
			}
		});
	} else {
		$('#cour_charge').val("");
		$('#total_charge').val("");
	}
}
</script>


<div id="title">Inventory Management</div>

<form name="inventory-form" method="post" action="">
<table border="0" cellspacing="3" cellpadding="3" align="center">
	<tr>
		<td colspan=2>
			<?php
				se();
			?>
		</td>
	</tr>
	<tr>
		<td align=right>Booking Date:</td>
		<td>
			<?php
				$booking_date = ($booking_date == "") ? date('d-m-Y') : $booking_date;
			?>	
			<input name="booking_date" type="text" value="<?php echo $booking_date?>" onfocus="popcal(this)" />
		</td>
	</tr>
	<tr>
		<td align=right>Company Name:</td>
		<td>
			<select name="company_id" id="company_id" onchange='GetCharge()'>
			<?php
				sopt($company_arr, $company_id)
			?>
			</select>
		</td>
	</tr>
	<tr>
		<td align=right>C/N No.:</td>
		<td>
			<input name="cn_no" type="text" value="<?php echo $cn_no?>"/>
			To
			<?php $st = (trim($btval) == 'Update') ? 'readonly' : ''; ?>
			<input name="cn_no_to" type="text" value="1" size="5" <?php echo $st ?> />
		</td>
	</tr>
	<tr>
		<td align=right>Invoice No.:</td>
		<td>
			<input name="invoice_no" type="text" size="10" value="<?php echo $invoice_no?>"/>
			Value
			<input name="invoice_value" type="text" size="10" value="<?php echo $invoice_value ?>" />
		</td>
	</tr>
	<tr>
		<td align=right>Destination:</td>
		<td>
			<select name="destination_id" id="destination_id" onchange='GetCharge()'>
			<?php
				sopt($destination_arr, $destination_id)
			?>
			</select>
		</td>
	</tr>
	<tr>
		<td align=right>Slab Type:</td>
		<td>
			<select name="document_id" id="document_id" onchange='GetCharge()'>
			<?php
				sopt($slab_type_arr, $document_id)
			?>
			</select>
		</td>
	</tr>
	<tr>
		<td align=right><!-- Slab For: --></td>
		<td>
                    <select name="slab_for" id="slab_for" onchange='GetCharge()' style="display: none">
			<?php
				sopt($slab_for_arr, $slab_for)
			?>
			</select>
		</td>
	</tr>
	<tr>
		<td align=right>Weight:</td>
		<td>
			<input name="weight" id="weight" type="text" value="<?php echo $weight?>" onblur='GetCharge()'/>
			<select name="weight_unit" id="weight_unit" onchange='GetCharge()'>
                            <option value="1">gms</option>
			<?php
				#sopt($weight_unit_arr, $weight_unit)
			?>
			</select>
		</td>
	</tr>
	<tr>
		<td align=right>Courier Charge:</td>
		<td><input name="cour_charge" id="cour_charge" type="text" value="<?php echo $cour_charge?>" /></td>
	</tr>
	<tr>
		<td align=right>Total Charge:</td>
		<td><input name="total_charge" id="total_charge" type="text" value="<?php echo $total_charge?>" /></td>
	</tr>
	<tr>
		<td align=right>Status:</td>
		<td>
			<?php
				if($status == "")
					$status = 1;
				foreach($status_arr as $k => $v) {
					$st = ($status == $k) ? 'checked' : '';
					echo "&nbsp;&nbsp;<input type='radio' name='status' value='$k' $st> ".$v;
				}
			
			?>
		</td>
	</tr>
	<tr>
		<td colspan="2" align="center">
			<input type=hidden name=id value="<?php echo $id?>" />
			<input type=hidden name=step value="<?php echo $step?>" />
			<input type="submit" name="submit" value="<?php echo $btval?>" class='button'/>
		</td>
	</tr>	
</table>
</form>
<?php
echo "<hr />";
$table = "inventories";
$cols = "id, company_id as `Company`, date_format(booking_date ,'%d-%m-%Y') as `Date`, cn_no, (select destination from destinations where id = destination_id) as `Destination`, document_id as `Type`, total_charge as `Total Charge`, status ";
$orderby = ($orderby == '') ? "id" : $orderby;
$sort = ($sort == '') ? "desc" : $sort;
$pagelength = 100;
$reparr = array();
if(is_array($company_arr)){
	foreach($company_arr as $k => $v){
		$reparr['Company'][$k] = $v;	
	}
}	
foreach($slab_type_arr as $k => $v){
	$reparr['Type'][$k] = $v;	
}
foreach($weight_unit_arr as $k => $v){
	$reparr['weight_unit'][$k] = $v;	
}
foreach($status_arr as $k => $v){
	$reparr['status'][$k] = $v;	
}
gettable("$table",$cols,"$where","$orderby","$sort",$pg,"<a href='?eid=\$id&submit=Update' style='text-decoration:none'>Update</a>&nbsp;|&nbsp;<a href=\"?eid=\$id&submit=Delete\" onclick=\"javascript:return confirm('Are you sure you want to Delete?')\" style='text-decoration:none'>Delete</a>",Array("id"),$reparr);
?>